package com.sskj.mine.ui.activity;


import android.support.v4.widget.NestedScrollView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.TimeUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.AddressBean;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.bean.WithdrawSxf;
import com.sskj.mine.presenter.MineWithdrawActivityPresenter;
import com.sskj.common.rxbus2.RxBus;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

@Route(path = RConfig.MINE_WITHDRAW)
public class MineWithdrawActivity extends BaseActivity<MineWithdrawActivityPresenter> {
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.left_img)
    ImageView leftImg;
    @BindView(R2.id.left_tv)
    TextView leftTv;
    @BindView(R2.id.ivBack)
    LinearLayout ivBack;
    @BindView(R2.id.right_tv)
    TextView rightTv;
    @BindView(R2.id.ivRight)
    ImageView ivRight;
    @BindView(R2.id.available_tv)
    TextView availableTv;
    @BindView(R2.id.address_edt)
    EditText addressEdt;
    @BindView(R2.id.address_img)
    ImageView addressImg;
    @BindView(R2.id.num_edt)
    EditText numEdt;
    @BindView(R2.id.unit_tv)
    TextView unitTv;
    @BindView(R2.id.tvAll)
    TextView tvAll;
    @BindView(R2.id.max_num_tv)
    TextView maxNumTv;
    @BindView(R2.id.tvFee)
    TextView tvFee;
    @BindView(R2.id.code_edt)
    EditText codeEdt;
    @BindView(R2.id.get_code_tv)
    TextView getCodeTv;
    @BindView(R2.id.submit_btn)
    Button submitBtn;
    @BindView(R2.id.account_list)
    NestedScrollView accountList;
    private Disposable timeDis;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mine_withdraw;
    }

    @Override
    public MineWithdrawActivityPresenter getPresenter() {
        return new MineWithdrawActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        RxBus.getDefault().register(this);
        tvTitle.setText(App.INSTANCE.getString(R.string.mine_mineWithdrawActivity1_j));
        rightTv.setText(App.INSTANCE.getString(R.string.mine_mineWithdrawActivity2_j));
        rightTv.setVisibility(View.VISIBLE);
        ivRight.setVisibility(View.VISIBLE);
        ivRight.setImageDrawable(getResources().getDrawable(R.mipmap.mine_coin_record));
        ClickUtil.click(ivRight, () -> {
            ARouter.getInstance().build(RConfig.MINE_ASSET_RECORDS).withInt(Constans.TYPE, 0).navigation();
        });
        ClickUtil.click(rightTv, () -> {
            ARouter.getInstance().build(RConfig.MINE_ASSET_RECORDS).withInt(Constans.TYPE, 0).navigation();
        });
        ClickUtil.click(getCodeTv, () -> {
            String mobile = SPUtil.get(SPConfig.MOBILE, "");
            mPresenter.sendSms(mobile);
        });
        ClickUtil.click(addressImg, () -> {
            ARouter.getInstance().build(RConfig.WALLET_ADDRESS_BOOK).withBoolean(Constans.IS_SELECT,true).navigation();
        });

    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter.getSxf();
    }

    public void updateUI(WithdrawSxf data) {
        maxNumTv.setText("0" + "CQTF");
        numEdt.setHint(String.format(App.INSTANCE.getString(R.string.mine_mineWithdrawActivity3_j), data.getTb_min()));
        availableTv.setText(data.getUsable());
        tvFee.setText(String.format(App.INSTANCE.getString(R.string.mine_mineWithdrawActivity4_j), data.getSxfee()));
        ClickUtil.click(submitBtn, () -> {
            if (checkParam()) {
                mPresenter.withdraw(numEdt.getText().toString(),
                        codeEdt.getText().toString(),
                        addressEdt.getText().toString()
                );
            }
        });
        ClickUtil.click(tvAll, () -> {
            numEdt.setText(data.getUsable());
        });
        RxTextView.afterTextChangeEvents(numEdt)
                .subscribe(textViewAfterTextChangeEvent -> {
                    String s = textViewAfterTextChangeEvent.editable().toString();
                    if (TextUtils.isEmpty(s)) {
                        maxNumTv.setText("0");
                        return;
                    }
                    double v = Double.valueOf(s) - Double.valueOf(data.getSxfee());
                    v = Math.max(0, v);
                    maxNumTv.setText(String.valueOf(v));
                });
    }

    private boolean checkParam() {

        if (TextUtils.isEmpty(addressEdt.getText())) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineWithdrawActivity5_j));
            return false;
        }
        if (TextUtils.isEmpty(numEdt.getText())) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineWithdrawActivity6_j));
            return false;
        }
        if (TextUtils.isEmpty(codeEdt.getText())) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineWithdrawActivity7_j));
            return false;
        }
        return true;
    }

    public void sendCheckCodeSuccess() {
        DisposUtil.close(timeDis);
        timeDis = TimeUtil.newTime(this)
                .subscribe(aLong -> {
                    if (aLong > 0) {
                        getCodeTv.setText(aLong + "s");
                    } else {
                        getCodeTv.setText(App.INSTANCE.getString(R.string.mine_mineRegisterActivity4_j));
                        getCodeTv.setEnabled(true);
                    }
                });
    }


    @Override
    protected void onDestroy() {
        DisposUtil.close(timeDis);
        super.onDestroy();
    }

    public void sendCheckCodeFail() {
        getCodeTv.setEnabled(true);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateAddress(AddressBean addressBean) {
        if (addressEdt != null) {
            addressEdt.setText(addressBean.getAddress());
        }
    }
}
